package com.earthea.common.jackson;

import com.earthea.common.annotation.Anonymize;
import com.earthea.common.enumeration.AnonymizeType;
import com.earthea.common.util.JacksonUtils;
import lombok.Data;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class AnonymizeSerializerTest {

    @Test
    public void test() {
        TestBean testBean = new TestBean();
        testBean.setName("name");
        testBean.setPassword("password");
        testBean.setPhone1("phone");
        testBean.setPhone2("15299998888");
        testBean.setEmail1("1@qq.com");
        testBean.setEmail2("22@qq.com");
        testBean.setEmail3("3333@qq.com");
        testBean.setDefault1("1");
        testBean.setDefault2("22");
        testBean.setDefault3("3".repeat(5));
        testBean.setDefault4("4".repeat(8));
        testBean.setDefault5("5".repeat(12));
        testBean.setDefault6("6".repeat(20));
        assertThat(JacksonUtils.toJsonAnonymize(testBean)).isEqualTo("{\"name\":\"name\",\"password\":\"******\",\"phone1\":\"p****e\",\"phone2\":\"152****8888\",\"email1\":\"1@****om\",\"email2\":\"2*****@qq.com\",\"email3\":\"33****@qq.com\",\"default1\":\"******\",\"default2\":\"2*****\",\"default3\":\"3****3\",\"default4\":\"44****44\",\"default5\":\"555****555\",\"default6\":\"6666****6666\"}");
    }

    @Data
    private static class TestBean {
        private String name;

        @Anonymize(type = AnonymizeType.PASSWORD)
        private String password;

        @Anonymize(type = AnonymizeType.PHONE)
        private String phone1;

        @Anonymize(type = AnonymizeType.PHONE)
        private String phone2;

        @Anonymize(type = AnonymizeType.EMAIL)
        private String email1;

        @Anonymize(type = AnonymizeType.EMAIL)
        private String email2;

        @Anonymize(type = AnonymizeType.EMAIL)
        private String email3;

        @Anonymize(type = AnonymizeType.DEFAULT)
        private String default1;

        @Anonymize(type = AnonymizeType.DEFAULT)
        private String default2;

        @Anonymize(type = AnonymizeType.DEFAULT)
        private String default3;

        @Anonymize(type = AnonymizeType.DEFAULT)
        private String default4;

        @Anonymize(type = AnonymizeType.DEFAULT)
        private String default5;

        @Anonymize(type = AnonymizeType.DEFAULT)
        private String default6;
    }
}